package com.example.administrator.q1;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

public class sliterActivity extends AppCompatActivity {

    private int picId = 0;

    ViewPager viewPager;
    int[] myViewId = new int[] {R.layout.viewpager_one, R.layout.viewpager_two, R.layout.viewpager_three, R.layout.viewpager_four};
    List<View> myList = new ArrayList<View>();

    LinearLayout linearLayout;
    List<ImageView> mDots = new ArrayList<ImageView>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sliter);

        // 获得linearyout
        linearLayout = (LinearLayout)findViewById(R.id.linearLayout);

        // 设置linearLayout
        for (int i = 0; i < myViewId.length; i++) {
            ImageView imageView = new ImageView(sliterActivity.this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(50, 50));
            imageView.setPadding(5, 5, 5, 5);
            if (i == 0)
                imageView.setImageDrawable(getResources().getDrawable(R.mipmap.small_dot));
            else
                imageView.setImageDrawable(getResources().getDrawable(R.mipmap.white_dot));
            mDots.add(imageView);
            linearLayout.addView(imageView);
        }
        linearLayout.setHovered(true);
        // mDots.get(0).setImageResource(R.mipmap.white_dot);
        // mDots.get(0).setImageDrawable(getResources().getDrawable(R.mipmap.small_dot));
        //

        for (int i = 0; i < myViewId.length; i++) {
            View view = getLayoutInflater().inflate(myViewId[i], null);
            myList.add(view);
        }

        // 设置ViewPager属性
        viewPager = (ViewPager)findViewById(R.id.ViewPager_two);
        viewPager.setAdapter(new MyAdapter());
        viewPager.addOnPageChangeListener(new MyOnPageChangeListener());

    }
    class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return myViewId.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(myList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = myList.get(position);
            container.addView(view);
            myList.add(view);
            return view;
        }
    }

    public void enter_OnClick(View view) {
        Intent intent = new Intent();
        intent.setClass(sliterActivity.this, MainActivity.class);
        startActivity(intent);
        // finish();
    }

    private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            // 交换位置完成转换
            ImageView imageView = new ImageView(sliterActivity.this);
            imageView = mDots.get(picId);
            mDots.get(picId).setImageResource(R.mipmap.white_dot);
            mDots.get(i).setImageResource(R.mipmap.small_dot);
            picId = i;
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    }
}
